<script>
  import { reactive } from 'vue'
  import {
    save,
    saveOutsideAsthmaAndAllergicForm,
  } from '@/api/modules/reception'
  import { ElMessage } from 'element-plus'
  export default function () {
    // 初始化筛查表字段
    const getForm = function () {
      return {
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        attr5: null,
        attr6: null,
        attr7: null,
        attr8: null,
        attr9: null,
      }
    }
    const pageData = function () {
      return reactive({ title: '', form: getForm(), disabled: false })
    }
    const rulesTemplate = function () {
      const rules = {
        attr1: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr2: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr3: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr4: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr5: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr6: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr7: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr8: [{ required: true, message: '请完善选项', trigger: 'change' }],
        attr9: [{ required: true, message: '请完善选项', trigger: 'change' }],
      }
      return rules
    }

    const pageDataToOther = function () {
      return reactive({ title: '', form: {}, disabled: false })
    }
    const rulesTemplateToOther = function () {
      const rules = {
        q1: [{ required: true, message: '请完善选项', trigger: 'change' }],
        q2: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q3: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q4: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q5: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q6: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q7: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q8: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q9: [{ required: false, message: '请完善选项', trigger: 'change' }],
        q10: [{ required: false, message: '请完善选项', trigger: 'change' }],
      }
      return rules
    }
    const toSave = function (type) {
      return (formRef, form, config) => {
        return new Promise((resolve, reject) => {
          formRef.value.validate((valid) => {
            if (valid) {
              if (type) {
                saveOutsideAsthmaAndAllergicForm({ ...form, ...config })
                  .then((res) => {
                    resolve(res)
                  })
                  .catch((err) => {
                    reject(err)
                  })
              } else {
                save({ ...form, ...config })
                  .then((res) => {
                    resolve(res)
                  })
                  .catch((err) => {
                    reject(err)
                  })
              }
            } else {
              reject('请填写完毕')
              ElMessage.warning('请填写完毕')
            }
          })
        })
      }
    }
    return {
      getForm,
      pageData,
      rulesTemplate,
      toSave,
      rulesTemplateToOther,
      pageDataToOther,
    }
  }
</script>
